package com.bs.teachassistant.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bs.teachassistant.R;
import com.bs.teachassistant.entity.Note;
import com.bs.teachassistant.utils.FormatUtils;
import com.bs.teachassistant.utils.GsonUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by limh on 2020/5/2.
 * 添加日志
 */
@ContentView(R.layout.activity_add_record)
public class AddRecordActivity extends BaseActivity {
    @ViewInject(R.id.txt_record_title)
    TextView txtTitle;
    @ViewInject(R.id.txt_record_time)
    TextView txtTime;
    @ViewInject(R.id.image_record_finish)
    TextView txtFinish;
    @ViewInject(R.id.edit_note_title)
    EditText editTitle;
    @ViewInject(R.id.edit_note_remark)
    EditText editRemark;
    @ViewInject(R.id.edit_id_note_content)
    EditText editContent;
    @ViewInject(R.id.line_edit)
    LinearLayout lineEdit;

    List<Note> allNotes;
    private Note note;

    @Override
    public void initViews() {
        note = getIntent().getParcelableExtra("note");
        if (null != note) {
            txtTitle.setText(note.getCalssify());
            txtTime.setText(note.getTime());
            editTitle.setText(note.getTitle());
            editRemark.setText(note.getRemark());
            editContent.setText(note.getContent());

            editTitle.setEnabled(false);
            editRemark.setEnabled(false);
            editContent.setEnabled(false);
            txtFinish.setVisibility(View.INVISIBLE);
            lineEdit.setVisibility(View.VISIBLE);
        } else {
            txtTitle.setText(getIntent().getStringExtra("title"));
            txtTime.setText(FormatUtils.getDateTime());
        }
        allNotes = new ArrayList<>();
    }

    @Override
    public void initDatas() {
        if (!TextUtils.isEmpty(userPreference.getString("notes", ""))) {
            allNotes = GsonUtils.NoteGsonToBean(userPreference.getString("notes", ""));
        }
    }

    @Event(value = {R.id.image_record_close, R.id.image_record_finish,
            R.id.txt_edit, R.id.txt_delete}, type = View.OnClickListener.class)
    private void OnClick(View view) {
        switch (view.getId()) {
            case R.id.image_record_close:
                finish();
                overridePendingTransition(R.anim.push_right_in,
                        R.anim.push_right_out);
                break;
            case R.id.image_record_finish:
                saveNote();
                break;
            case R.id.txt_edit:
                editTitle.setEnabled(true);
                editRemark.setEnabled(true);
                editContent.setEnabled(true);
                txtFinish.setVisibility(View.VISIBLE);
                for (Note item : allNotes) {
                    if (item.getTime().equals(note.getTime())) {
                        allNotes.remove(item);
                        return;
                    }
                }
                break;
            case R.id.txt_delete:
                Log.w("NOTES", "NOTE SIZE=" + allNotes.size());
                for (Note item : allNotes) {
                    if (item.getTime().equals(note.getTime())) {
                        allNotes.remove(item);
                        Log.w("NOTES", "NOTE SIZE=" + allNotes.size());
                        Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show();
                        userPreference.edit().putString("notes", GsonUtils.GsonString(allNotes)).apply();
                        finish();
                        overridePendingTransition(R.anim.push_right_in,
                                R.anim.push_right_out);
                        return;
                    }
                }

                break;
        }
    }

    private void saveNote() {
        if (TextUtils.isEmpty(editTitle.getText().toString())) {
            Toast.makeText(this, "标题不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(editContent.getText().toString())) {
            Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        Note note = new Note(txtTitle.getText().toString(), editTitle.getText().toString(), editRemark.getText().toString(),
                editContent.getText().toString(), txtTime.getText().toString());
        allNotes.add(note);
        userPreference.edit().putString("notes", GsonUtils.GsonString(allNotes)).apply();
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        finish();
        overridePendingTransition(R.anim.push_right_in,
                R.anim.push_right_out);
    }
}
